package com.algorithm.ms1024In2021;


import java.util.*;

public class Test2 {
    //申请编号, (会议室id，开始时间，结束时间)
    static Map<Integer, List<Integer>> map = new HashMap<Integer, List<Integer>>();
    public static void main(String[] args) throws Exception{
        Scanner scanner = new Scanner(System.in);
        int n = Integer.parseInt(scanner.nextLine());
        int arr[][] = new int[n][1000000];
        for(int i=0;i<n;i++){
            for(int j=0;j<1000000;j++){
                arr[i][j]=0;
            }
        }
        while (true){
            String s = scanner.nextLine();
            if("-1".equals(s)){
                break;
            }
            String[] line = s.split(",");
            int id = Integer.parseInt(line[0]);
            int action = Integer.parseInt(line[1]);
            if(action==1){//取消
                int cancel = cancel(arr, id);
                System.out.println(cancel);
                continue;
            }
            int start = Integer.parseInt(line[2]);
            int end = Integer.parseInt(line[3]);
            if(action==0){//申请
                int qiang = qiang(arr, start, end);
                map.put(id, Arrays.asList(qiang-1, start,end));
                System.out.println(qiang);
            }
            if(action==2){//停止
                int stop = stop(arr, id, start, end);
                System.out.println(stop);
            }
        }
    }

    private static int qiang(int arr[][], int start, int end){
        int num = arr.length;
        List<Integer> kongyu = new ArrayList<Integer>();
        for(int n=0;n<num;n++){
            boolean has = false;
            for(int i=start;i<end;i++){
                if(arr[n][i]==1){
                    has = true;
                    break;
                }
            }
            if(!has){
                kongyu.add(n);
            }
        }
        if(kongyu.size()==0){
            return -1;
        }
        int lastTime[] = new int[kongyu.size()];
        for(int i=0;i<kongyu.size();i++){
            lastTime[i] = -1;
        }

        boolean hasMinTime = false;
        for(int i=0;i<kongyu.size();i++){
            for(int index=end;index<arr[0].length;index++){
                if(arr[kongyu.get(i)][index]==1){
                    lastTime[i] = index;
                    hasMinTime = true;
                    break;
                }
            }
        }
        if(!hasMinTime){
            Integer index = kongyu.get(0);
            fill(arr,index,start,end);
            return index+1;
        }
        int minTime = 9999999;
        int minIndex = -1;
        for(int i=0;i<lastTime.length;i++){
            if(lastTime[i]!=-1 && minTime>lastTime[i]){
                minTime = lastTime[i];
                minIndex = i;
            }
        }
        fill(arr,minIndex,start,end);
        return minIndex+1;
    }

    private static int cancel(int arr[][], int id){
        List<Integer> list = map.get(id);
        int index = list.get(0);
        for(int j=list.get(1);j<list.get(2);j++){
            arr[index][j] = 0;
        }
        return index+1;
    }

    private static int stop(int arr[][], int id, int start, int end){
        List<Integer> list = map.get(id);
        int index = list.get(0);
        for(int j=list.get(1);j<list.get(2);j++){
            arr[index][j] = 0;
        }
        for(int j=start;j<end;j++){
            arr[index][j] = 1;
        }
        return index + 1;
    }

    private static void fill(int arr[][], int index, int start, int end){
        for(int i=start;i<end;i++){
            arr[index][i]=1;
        }
    }
    private static void clear(int arr[][], int index, int start, int end){
        for(int i=start;i<end;i++){
            arr[index][i]=0;
        }
    }
}
